<template>
  <div class="success-container">
    <div class="success-card">
      <div class="success-icon">
        <i class="fa fa-check-circle"></i>
      </div>
      
      <h1 class="success-title">支付成功！</h1>
      
      <p class="success-message">您的订单已支付完成，感谢您的购买</p>
      
      <div class="order-info" v-if="orderId">
        <p>订单编号：<span>{{ orderId }}</span></p>
        <p>交易号：<span>{{ tradeId }}</span></p>
      </div>
      
      <div class="action-buttons">
        <button class="btn order-btn" @click="gotoOrderList">
          <i class="fa fa-list"></i> 查看我的订单
        </button>
        <button class="btn home-btn" @click="gotoHome">
          <i class="fa fa-home"></i> 返回首页
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';

// 路由实例
const router = useRouter();
const route = useRoute();

// 状态管理
const orderId = ref('');
const tradeId = ref('');

// 页面加载时获取支付结果
onMounted(() => {
  // 从URL参数中获取订单信息（支付宝回调返回的参数）
  const query = route.query;
  if (query.out_trade_no) {
    orderId.value = query.out_trade_no;
  }
  if (query.trade_no) {
    tradeId.value = query.trade_no;
    
    // 通知后端支付成功（如果需要）
    notifyPaymentSuccess(query);
  }
});

/**
 * 通知后端支付成功
 */
const notifyPaymentSuccess = async (paymentData) => {
  try {
    await axios.put('/order/order/status/', null, {
      params: {
        out_trade_no: paymentData.out_trade_no,
        trade_no: paymentData.trade_no
      }
    });
    console.log('支付状态已同步到服务器');
  } catch (err) {
    console.error('同步支付状态失败:', err);
    // 这里可以记录错误，稍后重试，不影响用户体验
  }
};

/**
 * 前往订单列表
 */
const gotoOrderList = () => {
  router.push('/order');
};

/**
 * 返回首页
 */
const gotoHome = () => {
  router.push('/home');
};
</script>

<style scoped>
.success-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f8f9fa;
  padding: 1rem;
}

.success-card {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
  padding: 2.5rem 2rem;
  max-width: 500px;
  width: 100%;
  text-align: center;
}

.success-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 1.5rem;
  background-color: #e8f5e9;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.success-icon i {
  font-size: 40px;
  color: #2e7d32;
}

.success-title {
  margin: 0 0 1rem 0;
  font-size: 1.8rem;
  color: #333;
}

.success-message {
  margin: 0 0 2rem 0;
  font-size: 1rem;
  color: #666;
  line-height: 1.5;
}

.order-info {
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 1.2rem;
  margin-bottom: 2rem;
  text-align: left;
}

.order-info p {
  margin: 0 0 0.8rem 0;
  font-size: 0.95rem;
}

.order-info p:last-child {
  margin-bottom: 0;
}

.order-info span {
  color: #333;
  font-weight: 500;
  word-break: break-all;
}

.action-buttons {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.btn {
  flex: 1;
  padding: 0.8rem 0;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: all 0.3s;
}

.order-btn {
  background-color: white;
  color: #ff7a5a;
  border: 1px solid #ff7a5a;
}

.order-btn:hover {
  background-color: #fff5f5;
}

.home-btn {
  background-color: #ff7a5a;
  color: white;
}

.home-btn:hover {
  background-color: #e66a4c;
  transform: translateY(-2px);
}

@media (max-width: 480px) {
  .action-buttons {
    flex-direction: column;
  }
  
  .success-title {
    font-size: 1.5rem;
  }
}
</style>
